package com.llmcu.msb.architect.concurrent.basic;

import java.util.concurrent.TimeUnit;

/**
 * 感受interrupt()与isInterrupted()
 *
 * @author liuling
 * @date 2024/9/17 18:09
 */
public class InterruptAndIsInterrupted {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            while (true) {
                // 根据方法注释，sleep抛异常后会重置打断标志，isInterrupted方法无法查询到中断标志，不会打印等号。所以注释掉sleep部分代码
//                try {
//                    TimeUnit.SECONDS.sleep(1);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                System.out.println("..................");
                // 会一直打印等号
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("========================");
                }
            }
        });
        thread.start();
        TimeUnit.SECONDS.sleep(2);
        thread.interrupt();

    }
}
